<?php
App::uses('AppController', 'Controller');
/**
 * Configurations Controller
 *
 * @property Configuration $Configuration
 */
class ConfigurationsController extends AppController {
	public $helpers = array('FileUpload.FileUpload');
	public $components = array('FileUpload.FileUpload');
	
	public $cacheAction = false;
	
	public function beforeFilter(){
		parent::beforeFilter();
// 		$this->FileUpload->uploadDir('files'.DS.'upload'.DS.'images');
	}

	public function beforeRender(){
		parent::beforeRender();
		$this->layout = 'panel';
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$options['order'] = 'Configuration.cfg_group_id';
		$options['limit'] = 60;
		$this->paginate = $options;
		$this->Configuration->recursive = 0;
		$this->set('configurations', $this->paginate());
	}
	
	public function admin() {
		if ($this->request->is('post') || $this->request->is('put')) {
			foreach ($this->request->data['Configuration'] as $key => $value){
				$c['Configuration'] = $value;
				$this->Configuration->save($c);
			}
			$this->redirect($this->referer());
		}
		$options['order'] = 'Configuration.cfg_group_id';
		$options['limit'] = 60;
		$options['conditions'] = array('Configuration.cfg_group_id'=>9999);
		$this->paginate = $options;
		$this->Configuration->recursive = 0;
		$this->set('configurations', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Configuration->id = $id;
		if (!$this->Configuration->exists()) {
			throw new NotFoundException(__d('configuration','Invalid configuration'));
		}
		$this->set('configuration', $this->Configuration->read(null, $id));
	}

	public function install($id = null){
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])){
			if($this->Configuration->install($this->passedArgs['d'])){
				$this->Session->setFlash(__d('configuration','The configuration has been saved'));
			}else{
				$this->Session->setFlash(__d('configuration','The configuration could not be saved. Please, try again.'));
			}		
		}
		$this->redirect($this->referer());
	}
	
/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			
			if($this->FileUpload->success){
				$this->request->data['Configuration']['upload_id'] = $this->Configuration->Upload->getLastInsertID();
			}else{
				$this->Session->setFlash($this->FileUpload->showErrors());
			}
			
			$this->Configuration->create();
			if ($this->Configuration->save($this->request->data)) {
				$this->Session->setFlash(__d('configuration','The configuration has been saved'));
				if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])) $this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'settings'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('configuration','The configuration could not be saved. Please, try again.'));
			}
		}
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])){
			$this->request->data['Configuration']['domain_id'] = $this->passedArgs['d'];
			$domains = $this->Configuration->Domain->find('list');
			$uploads = $this->Configuration->Upload->find('list');
			$this->set(compact('domains', 'uploads'));
			
		}else{
			$this->Session->setFlash(__d('configuration','The configuration value could not be saved. Please, select first domain'));
			$this->redirect($this->referer());
		}
	}
	
	public function admin_add(){
		if ($this->request->is('post')) {
				
			if($this->FileUpload->success){
				$this->request->data['Configuration']['upload_id'] = $this->Configuration->Upload->getLastInsertID();
			}else{
				$this->Session->setFlash($this->FileUpload->showErrors());
			}
				debug($this->request->data);
			$this->Configuration->create();
			if ($this->Configuration->save($this->request->data)) {
				$this->Session->setFlash(__d('configuration','The configuration has been saved'));				
				$this->redirect(array('action' => 'admin'));
			} else {
				$this->Session->setFlash(__d('configuration','The configuration could not be saved. Please, try again.'));
			}
		}

	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Configuration->id = $id;
		if (!$this->Configuration->exists()) {
			throw new NotFoundException(__d('configuration','Invalid configuration'));
		}
		if(isset($this->passedArgs['d']) && !empty($this->passedArgs['d'])){
			if ($this->request->is('post') || $this->request->is('put')) {
				
				if($this->FileUpload->success){
					$this->request->data['Configuration']['upload_id'] = $this->Configuration->Upload->getLastInsertID();
				}else{					
					//throw new NotFoundException($this->FileUpload->showErrors());
				}
				
				if ($this->Configuration->save($this->request->data)) {
					$this->Session->setFlash(__d('configuration','The configuration has been saved'));
				} else {
					$this->Session->setFlash(__d('configuration','The configuration could not be saved. Please, try again.'));
				}
				if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p'])) 
					$this->redirect(array('controller'=>'projects','action' =>'view',$this->passedArgs['p'],'p'=>'metas'));
				$this->redirect(array('controller'=>'domains','action' =>'view',$this->passedArgs['d'],'d'=>'settings'));
			} else {
				$this->request->data = $this->Configuration->read(null, $id);
			}
			
			$domains = $this->Configuration->Domain->find('list');
			$uploads = $this->Configuration->Upload->find('list');
			$this->set(compact('domains', 'uploads'));
			
		}else{
			$this->Session->setFlash(__d('configuration','The configuration value could not be saved. Please, select first domain'));
			$this->redirect($this->referer());
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Configuration->id = $id;
		if (!$this->Configuration->exists()) {
			throw new NotFoundException(__d('configuration','Invalid configuration'));
		}
		if ($this->Configuration->delete()) {
			$this->Session->setFlash(__d('configuration','Configuration deleted'));
			$this->redirect($this->referer());
		}
		$this->Session->setFlash(__d('configuration','Configuration was not deleted'));
		$this->redirect($this->referer());
	}
}
